//
//  VNExpressRssController.m
//  VNExpressRSS
//
//  Created by Vinh Huynh on 8/23/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "VNExpressRssController.h"
#import "RSSParser.h"
#import "RssCell.h" 
#import "News.h";
#import "HomeViewController.h"
#import "NewsDetailViewController.h"
 
@implementation VNExpressRssController
@synthesize rssArray;
@synthesize table;
@synthesize cell;
@synthesize newsSelected;
@synthesize rssLink;
@synthesize titleLabel;

#pragma mark -
#pragma mark View lifecycle

-(IBAction) back{
	[self.navigationController popViewControllerAnimated:YES];
}

-(id) initTextSelected:(NSString *)text{
	self.newsSelected = [NSString stringWithString:text];
	
	NSLog(@"%@", self.newsSelected);
	[titleLabel setText:self.newsSelected];
	return self;
}

-(void) loadNewsWithRssLink:(NSString*) link{
	RSSParser *parser = [[RSSParser alloc] init];
	[parser getNewsFromURL:link withDelegate:self];
	[parser release];
}

- (void)viewDidLoad {
	self.rssArray = [[NSMutableArray alloc]init];
	self.rssLink = [[NSString alloc] init];
	
    [super viewDidLoad];
}

-(void) didEndGetNews:(NSMutableArray *)array{
	[self.rssArray setArray:array];
	[self.table reloadData];
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return  [self.rssArray count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";    
    cell = (RssCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
		[[NSBundle mainBundle] loadNibNamed:@"RssCell" owner:self options:nil];
    }
	News *news =  [self.rssArray objectAtIndex:indexPath.row];
    if (news.thumbnailLink != @"") {
		cell.thumbnailImage.image = news.thumbnail;
		[cell.activity setHidden:YES];
	}
	else {
	cell.thumbnailImage.image = [UIImage imageNamed:@"nophoto.png"]; 
	}
	[cell.titleLabel setText:news.title];
	[cell.descriptionTextView setText:news.description];
	[cell.descriptionTextView setFont:[UIFont systemFontOfSize:12]];
    self.navigationItem.title = self.newsSelected;
	[news release];
    return cell;
} //UIActivityIndicatorView

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
	return 60;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	NewsDetailViewController *newsController = [[NewsDetailViewController alloc] init];
	[self.navigationController pushViewController:newsController animated:YES];
	News *news = [self.rssArray objectAtIndex:[indexPath row]];
	[newsController getNewsDetail:news];
}

#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}

- (void)dealloc {
	[super dealloc];
	self.rssLink = nil;
	self.rssArray = nil;
	self.table = nil;
	self.cell = nil;
	self.newsSelected = nil;
	self.titleLabel = nil;	
}

@end

